package com.ymatou.iapi.productmanager.testcase;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.productproperty.GetProductPropertyResp;
import com.ymatou.iapi.productmanager.parameter.productproperty.SpuPropertyDto;
import com.ymatou.iapi.productmanager.service.QueryProductPropertyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_QueryProductProperty {
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品SPU属性列表");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new QueryProductPropertyCall().getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductProperty_001() throws Exception {
		Logger.start(true,"获取商品SPU属性列表");
		try{
			int thirdCategoryId =1122;
			int spuId = 237330;
			
			JSONObject ret = new QueryProductPropertyCall().call(thirdCategoryId, spuId);
			
			checkQueryProductProperty(thirdCategoryId,spuId,ret);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductProperty_001_001() throws Exception {
		Logger.start(true,"获取商品SPU属性列表 spu =0,spu=-1");
		try{
			int thirdCategoryId =1122;
			
			JSONObject ret = new QueryProductPropertyCall().call(thirdCategoryId, 0);
			checkQueryProductProperty(thirdCategoryId,0,ret);
			
			
			JSONObject ret1 = new QueryProductPropertyCall().call(thirdCategoryId, -1);
			checkQueryProductProperty(thirdCategoryId,-1,ret1);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductProperty_002() throws Exception {
		Logger.start(true,"获取商品SPU属性列表,没有spu");
		try{
			int thirdCategoryId =1122;
			
			JSONObject ret = new QueryProductPropertyCall().call(thirdCategoryId, 0);
			
			checkQueryProductProperty(thirdCategoryId,0,ret);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductProperty_003() throws Exception {
		Logger.start(true,"获取商品SPU属性列表,不存在thirdId,没有spu");
		try{
			int thirdCategoryId =1122123;
			
			JSONObject ret = new QueryProductPropertyCall().call(thirdCategoryId, 0);
			
			Logger.verifyEquals(0, ret.getJSONObject("Data").getJSONArray("PropertyList").length(), "不存在thirdId,返回空数组");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	public static void checkQueryProductProperty(int thirdCategoryId,int spuId,JSONObject ret) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		GetProductPropertyResp actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), GetProductPropertyResp.class);
		
		GetProductPropertyResp tgtBean = getGetProductPropertyRespFromSql(thirdCategoryId,spuId);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "SPU属性检查");
		
	}
	
	
	
	public static GetProductPropertyResp getGetProductPropertyRespFromSql(int thirdCategoryId,int spuId){
		GetProductPropertyResp ppr = new GetProductPropertyResp();
		
		List<SpuPropertyDto> propertyList = new ArrayList<>();
		
		List<Map<String,Object>> spuPropInSqlList = intergratedProductMapper.selectSpuPropertyByThirdCategoryId(thirdCategoryId);
		for (int i=0;i<spuPropInSqlList.size();i++){
			Map<String,Object> spuPropInSql = spuPropInSqlList.get(i);
			
			SpuPropertyDto spuPropertyDto = new SpuPropertyDto();
			spuPropertyDto.setPropertyId((Integer)spuPropInSql.get("PropertyId"));
			spuPropertyDto.setPropertyName(spuPropInSql.get("Name").toString());
			spuPropertyDto.setPropertyValueType((Integer)spuPropInSql.get("Styles"));
			spuPropertyDto.setIsRequired((Boolean)spuPropInSql.get("IsRequired"));
			spuPropertyDto.setIsMultiple((Boolean)spuPropInSql.get("MultiSelect"));
			spuPropertyDto.setInputValues(new ArrayList<>());

			List<Map<String,Object>> spuPropValInSqlList = intergratedProductMapper.selectSpuPropertyValByPropId((Integer)spuPropInSql.get("PropertyId"));
			spuPropertyDto.setValues(spuPropValInSqlList.stream().map(x->x.get("Value").toString()).collect(Collectors.toList()));
			propertyList.add(spuPropertyDto);
		}
		
		ppr.setPropertyList(propertyList);
		
		
		if (spuId>0){
			ppr.setSpuName(intergratedProductMapper.selectSpuProductBySpuId(spuId).get("ProductName").toString());
		}else{
			ppr.setSpuName(null);
		}
		
		return ppr;
	}
	
	
	public static void main(String[] args) {
		List<Map<String,Object>> spuPropValInSqlList = intergratedProductMapper.selectSpuPropertyValByPropId(26);
		String value = spuPropValInSqlList.stream().map(x->x.get("Value").toString()).collect(Collectors.joining(","));
		System.out.println(value);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
